package com.ziyia.a8_4_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {

    private static final String TAG = MainActivity.class.getName();
    private GestureDetector gestureDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this, this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    /**
     * Toast
     * @param context
     * @param msg
     */
    public static void showMsg(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }



    /**
     * 当触碰事件按下时触发该方法
     * @param e 封装了触碰的位置等信息
     * @return
     */
    @Override
    public boolean onDown(MotionEvent e) {
//        Log.d(TAG, "onDown() called with: e = [" + e + "]");
        return false;
    }

    /**
     * 当用户手指在触屏上按下，而且还未移动和松开时触发该方法
     * @param e
     */
    @Override
    public void onShowPress(MotionEvent e) {
//        Log.d(TAG, "onShowPress() called with: e = [" + e + "]");
    }

    /**
     * 用户手指在触摸屏上轻击事件将会触发该方法
     * @param e
     * @return
     */
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.d(TAG, "onSingleTapUp() called with: e = [" + e + "]");
        return false;
    }

    /**
     * 当用户手指上屏幕上滚动时，触发该方法
     * @param e1 位置信息
     * @param e2 位置信息
     * @param distanceX 从横向滚动的速度
     * @param distanceY 从纵向滚动的速度
     * @return
     */
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.d(TAG, "onScroll() called with: distanceX = [" + distanceX + "], distanceY = [" + distanceY + "]");
        return false;
    }

    /**
     * 当用户手指上触屏上长按时，触发该方法
     *
     * @param e
     */
    @Override
    public void onLongPress(MotionEvent e) {
        Log.d(TAG, "onLongPress() called with: e = [" + e + "]");
    }

    /**
     * 当用户手指在触屏上 “拖过” 时触发该方法
     *
     * @param e1
     * @param e2
     * @param velocityX 拖动动作在黄向的速度
     * @param velocityY 手动动作在纵向的速度
     * @return
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//        Log.d(TAG, "onFling() called with: e1 = [" + e1 + "], e2 = [" + e2 + "], velocityX = [" + velocityX + "], velocityY = [" + velocityY + "]");
//        Log.d(TAG, "onFling() called with: velocityX = [" + velocityX + "], velocityY = [" + velocityY + "]");
        return false;
    }

    public void scaleImage(View view) {
        actionIntent(MainActivity2.class, false);
    }

    public void scaleTextView(View view) {
        actionIntent(MainActivity3.class, false);
    }

    private void actionIntent(Class mClass, boolean flag) {
        Intent intent = new Intent(this, mClass);
        startActivity(intent);
        if (flag) finish();
    }
}